﻿using System.Collections.Generic;
using Mal.Balades.Admin.Services;

namespace Mal.Balades.Admin.Repositories
{
    public class InformationTypeRepository : RepositoryBase
    {
        #region Fields
        IAdminService service;
        private List<IInformationType> informationsTypes;

         
        #endregion

        #region Construction / Destruction
         public InformationTypeRepository()
        {
        }

         ~InformationTypeRepository()
        {
            Cleanup();
        }
        #endregion

        #region enum, struct, class

        #endregion

        #region Properties
         public List<IInformationType> Datas
         {
             get { return informationsTypes; }
             private set { informationsTypes = value; }
         }  
        #endregion

        #region Methods
         public void Cleanup()
         {
             if (this.service != null)
             {
                 service.GetInformationTypesCompleted -= new ServiceResponse<List<IInformationType>>(OnGetInformationTypesCompleted);
                 this.service.Cleanup();
                 this.service = null;
             }
         }

         private void Initialize()
         {
             informationsTypes = new List<IInformationType>();
             service = AdminServiceFactory.GetInstance();
             service.GetInformationTypesCompleted += new ServiceResponse<List<IInformationType>>(OnGetInformationTypesCompleted);
         }        

         protected override void LoadRepository()
         {
             Initialize();

             this.service.GetInformationTypesAsync();
         }

         
        #endregion

        #region Events      

        void OnGetInformationTypesCompleted(ServiceResponseArgs<List<IInformationType>> e)
        {
            this.Cleanup();

            if (e.Error != null)
                throw e.Error;

            this.informationsTypes = e.Result;

            this.LoadCompleted();
        }
        #endregion
       
    }
}
